package com.rupeng.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.rupeng.pojo.Permission;
import com.rupeng.service.PermissionService;
import com.rupeng.util.AjaxResult;
import com.rupeng.util.CommonUtils;

@Controller
@RequestMapping("/permission")
public class PermissionController {
	@Autowired
	private PermissionService permissionService;
	@RequestMapping("/list.do")
	public ModelAndView list() {
		List<Permission> permissions = permissionService.selectList();
		ModelAndView modelAndView = new ModelAndView("permission/list");
		modelAndView.addObject("permissions", permissions);
		return modelAndView;
	}
	@RequestMapping(value="/add.do",method=RequestMethod.GET)
	public ModelAndView addPage() {
		return new ModelAndView("permission/add");
	}
	@RequestMapping(value="/add.do",method=RequestMethod.POST)
	public @ResponseBody AjaxResult addSubmit(String path, String description) {
		if (CommonUtils.isEmpty(path) || CommonUtils.isEmpty(description)) {
			return AjaxResult.errorInstance("表格字段为空");
		}
		Permission permission = new Permission();
		permission.setPath(path);
		if (permissionService.isExisted(permission)) {
			return AjaxResult.errorInstance("该请求路径已存在");
		}
		permission = new Permission();
		permission.setDescription(description);
		if (permissionService.isExisted(permission)) {
			return AjaxResult.errorInstance("不允许使用与其他请求路径相同的描述");
		}
		permission = new Permission();
		permission.setPath(path);
		permission.setDescription(description);
		permissionService.insert(permission);
		return AjaxResult.successInstance("添加权限成功");
	}
	@RequestMapping(value="/update.do",method=RequestMethod.GET)
	public ModelAndView updatePage(Long id) {
		Permission permission = permissionService.selectOne(id);
		ModelAndView modelAndView = new ModelAndView("permission/update");
		modelAndView.addObject("permission", permission);
		return modelAndView;
	}
	@RequestMapping(value="/update.do",method=RequestMethod.POST)
	public @ResponseBody AjaxResult updateSubmit(Long id, String path, String description) {
		if (CommonUtils.isEmpty(path) || CommonUtils.isEmpty(description)) {
			return AjaxResult.errorInstance("表格字段为空");
		}
		Permission permission = new Permission();
		permission.setPath(path);
		permission = permissionService.selectOne(permission);
		if (permission!=null && permission.getId()==id) {
			return AjaxResult.errorInstance("该请求路径已存在");
		}
		permission = new Permission();
		permission.setPath(description);
		permission = permissionService.selectOne(permission);
		if (permission!=null && permission.getId()==id) {
			return AjaxResult.errorInstance("不允许使用与其他请求路径相同的描述");
		}
		permission = new Permission();
		permission.setPath(path);
		permission.setDescription(description);
		permissionService.update(permission);
		return AjaxResult.successInstance("修改权限成功");
	}
	@RequestMapping("/delete.do")
	public @ResponseBody AjaxResult delete(Long id) {
		permissionService.delete(id);
		return AjaxResult.successInstance("权限删除成功");
	}
}
